package com.mobileapp.baliairport.src.main;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

import com.mobileapp.baliairport.src.entity.fragment.FragmentArrayList;
import com.mobileapp.baliairport.src.listFlight.flightList;

public class fragmentFlightHeader extends Fragment {

	private GridView gridview;
	private flightList flightList;
	

	private Integer[] mThumbIds = { R.drawable.android_3, R.drawable.android_3,
			R.drawable.android_3, R.drawable.android_3 };

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.main, container, false);
		this.gridview = (GridView) view.findViewById(R.id.gridview);
		this.gridview.setAdapter(new MyAdapter(view.getContext()));
		this.gridview.setNumColumns(2);
		
		MainTabActivity.bar.setTitle("FLIGHT HEADER");
		MainTabActivity.titleback = "";
		for(int i=0;i<MainTabActivity.fragmentdb.size();i++){
			if(MainTabActivity.fragmentdb.get(i).getTitleBack().equals("FLIGHT HEADER")){
				FragmentArrayList frgArray = new FragmentArrayList();
				Fragment fragmentFlightHeader = new fragmentFlightHeader();
				frgArray.setFragment(fragmentFlightHeader);
				frgArray.setBackFragment(fragmentFlightHeader);
				frgArray.setTabName("FLIGHT");
				frgArray.setTitleBack("");
				MainTabActivity.fragmentdb.add(i, frgArray);
				MainTabActivity.bar.setDisplayHomeAsUpEnabled(false);
				break;
			}
		}
		
				
		
		
		gridview.setOnItemClickListener(new OnItemClickListener() {
			

			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				Toast.makeText(getActivity(), String.valueOf(position), Toast.LENGTH_SHORT).show();
				/*Fragment flightList = new flightList();
				FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
				transaction.add(R.layout.list_flight, flightList).commit();*/
				
				/*Fragment flightList = new flightList();
				FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
				transaction.add(R.id.listFlight, flightList).commit();*/
				
				
				if(position == 0){
					Fragment flightList = new flightList();
					Fragment fragmentFlightHeader = new fragmentFlightHeader();
					MainTabActivity tabActivity = (MainTabActivity) getActivity();
					MainTabActivity.titleback = "FLIGHT HEADER";
				    tabActivity.changeFragment(flightList,"FLIGHT INFORMATION - DOMESTIC AREA","FLIGHT","FLIGHT HEADER",fragmentFlightHeader);
				    
				}
				
				
				
				
				/*FragmentManager fragmentManager2 = getFragmentManager();
				FragmentTransaction fragmentTransaction2 = fragmentManager2.beginTransaction();
				Fragment flightList = new flightList();
				fragmentTransaction2.addToBackStack("xyz");
				fragmentTransaction2.hide(fragmentFlightHeader.this);
				fragmentTransaction2.add(android.R.id.content, flightList);
				fragmentTransaction2.commit();*/
			}
		});

		return view;
	}

	/*
	 * @Override public void onActivityCreated(Bundle savedInstanceState) {
	 * super.onCreate(savedInstanceState);
	 * 
	 * gridview = (GridView) findViewById(R.id.gridview);
	 * 
	 * itemsGridViewObj.setOnItemClickListener(new OnItemClickListener() {
	 * 
	 * @Override public void onItemClick(AdapterView<?> arg0, View arg1, int
	 * position, long arg3) {
	 * 
	 * Log.d(TAG, "--> onItemClick listener..."); // You should see this now
	 * if(position == 1) { FruitMenuFragment fruitMenuFragment = new
	 * FruitMenuFragment(); fragmentTransaction.replace(android.R.id.content,
	 * fruitMenuFragment); fragmentTransaction.commit(); } }}); }
	 */
	
	

	public class MyAdapter extends BaseAdapter {

		private Context mContext;

		public MyAdapter(Context context) {
			mContext = context;
		}

		@Override
		public int getCount() {
			return mThumbIds.length;
		}

		@Override
		public Object getItem(int arg0) {
			return mThumbIds[arg0];
		}

		@Override
		public long getItemId(int arg0) {
			return arg0;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {

			View grid;

			if (convertView == null) {
				grid = new View(mContext);
				LayoutInflater inflater = getLayoutInflater(getArguments());
				grid = inflater.inflate(R.layout.mygrid_layout, parent, false);
			} else {
				grid = (View) convertView;
			}

			ImageView imageView = (ImageView) grid.findViewById(R.id.image);
			imageView.setImageResource(mThumbIds[position]);

			return grid;
		}

	}

}
